package io.mft.dev.base.config;

import java.util.List;
import java.util.Map;

import io.mft.commons.config.ConfigProvider;
import io.mft.commons.config.ConfigProvider.CompletedLoadHandler;

public class LocalApplicationConfigProvideLoader extends LocalApplicationCustomizeConfigNamespaceConfigProvideLoader{
	public LocalApplicationConfigProvideLoader(ClassLoader classLoader) {
		super(classLoader, ConfigProvider.DEFAULT_CONFIG_NAMESPACE);
	}
	@Override
	public Map<String, Object> load() {
		String path=ConfigProvider.DEFAULT_CONFIG_NAMESPACE+".yml";
		Map<String, Object> configObjectMap=parseYaml(classLoader, path);
		return configObjectMap;
	}
	@Override
	public CompletedLoadHandler completedLoadHandler() {
		// TODO Auto-generated method stub
		return new CompletedLoadHandler() {
			
			@Override
			public void handle() {
				// TODO Auto-generated method stub
				LocalApplicationEnvConfigProvideLoader applicationEnvConfigProvideLoader=new LocalApplicationEnvConfigProvideLoader(classLoader);
				ConfigProvider.loadConfig(applicationEnvConfigProvideLoader);
				
				Map<String, Object> defaultNamespaceConfigObjectMap=ConfigProvider.getNamespaceConfigObject(classLoader, ConfigProvider.DEFAULT_CONFIG_NAMESPACE);
				List<String> customizeConfigNamespaces=ConfigProvider.getMapValue(defaultNamespaceConfigObjectMap, "customizeConfigNamespace");
				if (customizeConfigNamespaces!=null) {
					customizeConfigNamespaces.forEach(customizeConfigNamespace->{
						LocalApplicationCustomizeConfigNamespaceConfigProvideLoader applicationCustomizeConfigNamespaceConfigProvideLoader=new LocalApplicationCustomizeConfigNamespaceConfigProvideLoader(classLoader, customizeConfigNamespace);
						ConfigProvider.loadConfig(applicationCustomizeConfigNamespaceConfigProvideLoader);
					});
				}
			}
		};
	}
	
}
